package com.gopay.bis.khpay.ebank.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;

import com.gopay.bis.khpay.ebank.operation.KhPayQueryOrder;
import com.gopay.bis.khpay.ebank.operation.KhPayQueryTaskThread;
import com.gopay.bis.khpay.ebank.operation.KhPayRefund;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.KhPayConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.khpay.ebank.KhPayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelperKhPay;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 平安跨行支付接口处理
 * 
 * @KhPayBankServiceImpl.java
 * @author zhengxue
 * @2017-8-15 下午4:39:19 www.gopay.com.cn Inc.All rights reserved.
 */
public class KhPayBankServiceImpl implements BankService {
    private ICertHelperKhPay khPayCertHelper;
    private KhPayQueryOrder khPayQueryOrder;
    private KhPayRefund khPayRefund;
    @Autowired
    @Qualifier("taskExecutor")
    private TaskExecutor taskExecutor;

    @Override
    public Object sign(BaseMessage msg) throws GopayException {
        return khPayCertHelper.signData(msg);
    }

    @Override
    public Object verify(BaseMessage msg) throws GopayException {
        return khPayCertHelper.verify(msg);
    }

    @Override
    public Object query(Object obj) throws GopayException {
        KhPayMessage message = (KhPayMessage) obj;
        if (BisBankBusinessProd._12.value.equals(message.getBankBusProd().value)) {
            return khPayRefund.queryRefund(message);
        }
        return khPayQueryOrder.queryOrder(message);
    }

    @Override
    public Object refund(Object obj) throws GopayException {
        return khPayRefund.refund(obj);
    }

    @Override
    public Object dayend(Object obj) throws GopayException {
        KhPayMessage message = (KhPayMessage) obj;
        KhPayQueryTaskThread thread = new KhPayQueryTaskThread(message);
        taskExecutor.execute(thread);

        // 响应前端成功
        message.setRetCode(KhPayConstants.RES_CODE_SUCCESS);
        return message;
    }

    public void setKhPayCertHelper(ICertHelperKhPay khPayCertHelper) {
        this.khPayCertHelper = khPayCertHelper;
    }

    public void setKhPayQueryOrder(KhPayQueryOrder khPayQueryOrder) {
        this.khPayQueryOrder = khPayQueryOrder;
    }

    public void setKhPayRefund(KhPayRefund khPayRefund) {
        this.khPayRefund = khPayRefund;
    }

    public void setTaskExecutor(TaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }

}
